package org.yn.auth.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.auth.domain.Role;
import org.yn.auth.dto.RoleDTO;
import org.yn.auth.service.IRoleService;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.PageInfo;
import org.yn.query.RoleQuery;

import java.util.List;
import java.util.Objects;
@PermissionAnnotation(name="角色管理",descs = "角色管理")
@RestController
@RequestMapping("/role")
@Api("角色管理接口")
public class RoleController {


    @Autowired
    IRoleService roleService;

    /**
     * 新增
     */
    @PermissionAnnotation(name="新增或修改角色",descs = "新增或修改角色")
    @PostMapping()
    @ApiOperation(value = "新增或修改角色")
    public AjaxResult updateOrInsert(@RequestBody Role role) {
        try {
            if (Objects.nonNull(role)) {
                if (Objects.nonNull(role.getId())) {
                    roleService.update(role);
                } else {
                    roleService.insert(role);
                }
            } else {
                return AjaxResult.error("请输入正确的数据");
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    @PermissionAnnotation(name="查询所有角色",descs = "查询所有角色")
    @GetMapping()
    @ApiOperation(value = "查询所有角色")
    public AjaxResult selectAll() {
        try {
            List<Role> roleList = roleService.selectAll();
            return AjaxResult.success(roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询单个角色
     */
    @PermissionAnnotation(name="通过id查询角色",descs = "通过id查询角色")
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询角色")
    public AjaxResult selectById(@PathVariable Long id) {
        try {
            Role role = roleService.selectById(id);
            return AjaxResult.success(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 删除部门
     */
    @PermissionAnnotation(name="通过id删除角色",descs = "通过id删除角色")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除角色")
    public AjaxResult delete(@PathVariable Long id) {
        try {
            roleService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }



    /**
     * 分页查询接口
     */
    @PermissionAnnotation(name="角色的分页查询",descs = "角色的分页查询")
    @PutMapping
    @ApiOperation(value = "角色的分页查询")
    public AjaxResult queryPage(@RequestBody RoleQuery roleQuery){
        try {
            PageInfo<Role> pageInfo = roleService.queryPage(roleQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 批量删除部门
     * @param ids
     * @return
     */
    @PermissionAnnotation(name="批量删除角色",descs = "批量删除角色")
    @PatchMapping
    @ApiOperation(value = "批量删除角色")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            roleService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

//    @PermissionAnnotation(name="获取树",descs = "获取部门树")
//    @GetMapping("/deptTree")
//    @ApiOperation(value = "获取部门树")
//    public AjaxResult deptTree() {
//        try {
//            List<Department> list= roleService.getDeptTree();
//            return AjaxResult.success(list);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return AjaxResult.error(e.getMessage());
//        }
//    }

// 根据角色添加保存权限
    @PermissionAnnotation(name="根据角色添加保存权限",descs = "根据角色添加保存权限")
    @PostMapping("/permission")
    @ApiOperation(value = "根据角色添加保存权限")
    public AjaxResult insertByRoleId(@RequestBody RoleDTO roleDTO) {
        try {
            roleService.insertByRoleId(roleDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }
    //根据角色id查询对应的权限
    @PermissionAnnotation(name="根据角色id查询对应的权限",descs = "根据角色id查询对应的权限")
    @GetMapping("/permission/{id}")
    @ApiOperation(value = "根据角色id查询对应的权限")
    public AjaxResult selectByRoleId(@PathVariable("id") Long id){
        try {
            List<String> sns = roleService.selectByRoleId(id);
            return AjaxResult.success(sns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

}
